
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

//base class
#include "GRTCameraUp.h"

#include "GRTGLVisu.h"
#include "RTMatrix44.h"

//#include <QGLWidget>
#include <QtOpenGL\qgl.h>
#include "GRTMemoryLeak.h"
//constructor
GRTCameraUp::GRTCameraUp() : GRTICamera(CT_UP), _radius(0.2f), _diameter(0.4f)
{
	_translate = RTVector3f(0, 3, 0);
}

//destructor
GRTCameraUp::~GRTCameraUp(){}

void GRTCameraUp::preRender()
{
	glPushMatrix();

	glTranslatef(_translate.x, _translate.y, _translate.z);
}

//render the camera
void GRTCameraUp::Render()
{
	glDisable(GL_LIGHTING);
	glColor3f(1, 1, 1);

	glPointSize(15);
	glBegin(GL_POINTS);
		glVertex3i(0, 0, 0);
	glEnd();

	glEnable(GL_LIGHTING);
}

void GRTCameraUp::postRender()
{
	//if the sphere is selected, draw the surrounding box and the translation mark
	if(_selected)
	{
		VISU.drawSelectionBox(_diameter, _diameter, _diameter);
	}

	glPopMatrix();
}

//set the translation vector
void GRTCameraUp::setTranslate(const RTVector3f& t)
{
	_translate = t;
}

//set the rotation angles //Hack to disable rotation
void GRTCameraUp::setRotation(const RTVector3f& /*r*/)
{
}

//set the scaling factors //Hack to disable scaling
void GRTCameraUp::setScale(const RTVector3f& /*s*/){}

//translate the object's position by the vector
void GRTCameraUp::translate(const RTVector3f& t)
{
	_translate = _translate + t;
}

//rotate the objects using the vector //Hack to disable rotation
void GRTCameraUp::rotate(const RTVector3f& /*r*/){}

//add the vector as scale components //Hack to disable scaling
void GRTCameraUp::scale(const RTVector3f& /*s*/){}

//determine if the point v is a point of the geometry object
bool GRTCameraUp::isGRTGeometry(const RTVector3f& v) const
{
	//the error offset
	const float error = 0.1f;

	//calculate the world matrix of the primitive
	RTMatrix44 transform;
	transform.createTranslation(_translate);

	//RTMatrix44 transform = t;

	//get V in local coordinate of the sphere
	RTVector3f localV = v * transform.inverse();
	
	//get the distance between localV and the center of the sphere (0, 0, 0).
	float length = localV.norme();

	//add the error
	length -= error;

	//if the point is inside the sphere
	if (length <= 0.2f)
		return true;

	//the point is not the sphere
	return false;
}
